//
//  AddSizeViewController.m
//  quickBatchManager
//
//  Created by 王定波 on 15/9/22.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "TsczViewController.h"

@interface TsczViewController ()
{
    NSMutableArray *buttonList;
    BatchProductModel *bp;
    SaleOrderModel *sa;
    AAMenuBubbles *payMenu;
}
@end

@implementation TsczViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)initUI
{
    UIImage *image = _bgImage.image;
    image = [image stretchableImageWithLeftCapWidth:floorf(image.size.width/3) topCapHeight:floorf(image.size.height/3)];
    _bgImage.image=image;
    self.view.backgroundColor=[UIColor clearColor];
    
    [_closeBtn bk_whenTapped:^{
            if(_delegate!=nil && [_delegate respondsToSelector:@selector(tsczClose)])
            {
                [_delegate tsczClose];
            }
           [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self reloadData:YES];
    
    
    [_payTypeBtn bk_whenTapped:^{
        
        if([sa.tspayType isValid])
        {
            _payTypeLabel.hidden = YES;
            sa.tspayType= @"";
            sa.paidPrice = 0;
            sa.unpaidPrice = 0;
            _payField.text = @"0.00";
            [sa updateToDB];
        }else
        {
            if(payMenu) {
                payMenu = nil;
            }
            payMenu = [[AAMenuBubbles alloc] initWithPoint:CGPointMake(245, 230) radius:90 andBubbles:[[NSString stringWithFormat:@"%@%@",[CoreArchive strForKey:NSDEFAULT_ST_PAYMENT],[CoreArchive boolForKey:NSDEFAULT_ST_PAYMENT_DS ]?@"|代收":@""] componentsSeparatedByString:@"|"] inView:self.view];
            payMenu.bubbleRadius = 30;
            payMenu.delegate=self;
            [payMenu show];
        }
        
    }];
    
    
    
    _mlField.tag=1;
    _zkField.tag=2;
    _amountField.tag=4;
    _payField.tag=3;
    
    
    [self addTextFieldChange:_mlField];
    [self addTextFieldChange:_zkField];
    [self addTextFieldChange:_amountField];
    [self addTextFieldChange:_payField];

    
    
}


-(void) initData
{
    buttonList=[[NSMutableArray alloc] init];
    if(_type==TsczBatch)
    {
        bp=[BatchProductModel searchSingleWithWhere:@{@"batchNo":_batchNo} orderBy:nil];
    }else
    {
        sa = [SaleOrderModel searchSingleWithWhere:@{@"saleOrderNo":_orderNo} orderBy:nil];
    }
    
}

-(void) reloadData:(BOOL)isInit
{
    if(_type==TsczBatch)
    {
        if(isInit)
        {
            _payTypeLabel.text=bp.tspayType;
            _amountLabel.text=[ToolUtils priceToStr:bp.batchPrice];
            _zkLabel.text=_zkField.text=[ToolUtils priceToStr:bp.discount];
            _mlField.text=_moLabel.text=[ToolUtils priceToStr:bp.moLingPrice];
            _djLabel.text=_payField.text=[ToolUtils priceToStr:bp.paidPrice];
            _yeLabel.text=_amountField.text=[ToolUtils priceToStr:bp.deptPrice];
        }
        else
        {
            _zkLabel.text=[ToolUtils priceToStr:bp.discount];
            _moLabel.text=[ToolUtils priceToStr:bp.moLingPrice];
            _djLabel.text=[ToolUtils priceToStr:bp.paidPrice];
            _yeLabel.text=[ToolUtils priceToStr:bp.deptPrice];
            
            
            
        }
        bp.unpaidPrice=bp.totalPrice-bp.moLingPrice-bp.paidPrice-bp.deptPrice;
        [bp updateToDB];
        _payAmount.text=[ToolUtils priceToStr:bp.unpaidPrice];
    }else
    {
        if(isInit)
        {
            _payTypeLabel.text=sa.tspayType;
            _amountLabel.text=[ToolUtils priceToStr:sa.orderPrice];
            _zkLabel.text=_zkField.text=[ToolUtils priceToStr:sa.discount];
            _mlField.text=_moLabel.text=[ToolUtils priceToStr:sa.moLingPrice];
            _djLabel.text=_payField.text=[ToolUtils priceToStr:sa.paidPrice];
            _yeLabel.text=_amountField.text=[ToolUtils priceToStr:sa.deptPrice];
        }
        else
        {
            _zkLabel.text=[ToolUtils priceToStr:sa.discount];
            _moLabel.text=[ToolUtils priceToStr:sa.moLingPrice];
            _djLabel.text=[ToolUtils priceToStr:sa.paidPrice];
            _yeLabel.text=[ToolUtils priceToStr:sa.deptPrice];
            
            
            
        }
        sa.unpaidPrice=sa.totalPrice-sa.moLingPrice-sa.paidPrice-sa.deptPrice;
        [sa updateToDB];
        _payAmount.text=[ToolUtils priceToStr:sa.unpaidPrice];
    }
    
    
}

-(void)AAMenuBubbles:(AAMenuBubbles *)shareBubbles andIndex:(int)index;
{
    _payTypeLabel.hidden = NO;
    if(_type==TsczBatch)
    {
        _payTypeLabel.text=shareBubbles.bubblestrs[index];
        bp.tspayType=_payTypeLabel.text;
        [bp updateToDB];
    }else
    {
        _payTypeLabel.text=shareBubbles.bubblestrs[index];
        sa.tspayType=_payTypeLabel.text;
        [sa updateToDB];
    }
    
}

-(void)textFieldChanged:(id)sender
{
    UITextField *ctextfield=(UITextField*)sender;
    double amount=[ctextfield.text doubleValue];
    if(amount>=0 && ![ctextfield.text isBlank])
    {
       if(_type==TsczBatch)
       {
           switch (ctextfield.tag) {
               case 1:
                   bp.moLingPrice=amount;
                   break;
               case 2:
                   bp.discount=amount;
                   bp.totalPrice=bp.discount*bp.batchPrice;
                   break;
               case 3:
                   bp.paidPrice=amount;
                   break;
                   
               case 4:
                   bp.deptPrice=amount;
                   break;
                   
               default:
                   break;
           }
       }else
       {
           switch (ctextfield.tag) {
               case 1:
                   sa.moLingPrice=amount;
                   break;
               case 2:
                   sa.discount=amount;
                   sa.totalPrice=sa.discount*sa.orderPrice;
                   break;
               case 3:
                   sa.paidPrice=amount;
                   break;
                   
               case 4:
                   sa.deptPrice=amount;
                   break;
                   
               default:
                   break;
           }
       }
        [self reloadData:NO];
    }
}
@end
